package ext.opensource.netty.common.utils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class IpUtil {

	/***
	 * 获取外网IP
	 * 
	 * @return
	 */
	public static String getInternetIp() {
		try {

			final Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
			InetAddress inetAddress = null;
			Enumeration<InetAddress> inetAddresses = null;
			while (networks.hasMoreElements()) {
				inetAddresses = networks.nextElement().getInetAddresses();
				while (inetAddresses.hasMoreElements()) {
					inetAddress = inetAddresses.nextElement();
					if (inetAddress != null && inetAddress instanceof Inet4Address && !inetAddress.isSiteLocalAddress()
							&& !inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().indexOf(":") == -1) {
						return inetAddress.getHostAddress();
					}
				}
			}

			return null;

		} catch (final Exception e) {

			throw new RuntimeException(e);
		}
	}

	/**
	 * 获取内网IP
	 *
	 * @return
	 */
	public static String getIntranetIp() {
		try {
			return InetAddress.getLocalHost().getHostAddress();
		} catch (final Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 获取服务启动host
	 * 
	 * @return
	 */
	public static String getHost() {
		return IpUtil.getInternetIp() == null ? IpUtil.getIntranetIp() : IpUtil.getInternetIp();
	}

}
